package com.taotao.manage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
	@Autowired
	private ContentCategoryService contentCategoryService;
	/*
	 * 根据父节点查询内容分类
	 * url : '/rest/content/category',
		animate: true,
		method : "GET",;
	 * ContentCategory
	 */
	@RequestMapping(method=RequestMethod.GET)
public  ResponseEntity<List<ContentCategory>> queryContentCategory(
		@RequestParam(value="id",defaultValue="0") Long id){
		
		try {
			ContentCategory record = new ContentCategory();
			record.setParentId(id);
			List<ContentCategory> queryByContentCategoryList = this.contentCategoryService.queryByListWhere(record);
			if (null==queryByContentCategoryList) {
				//404没找到资源
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			//200返回资源
			return ResponseEntity.ok(queryByContentCategoryList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500异常
	return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	
}
	@RequestMapping(method=RequestMethod.POST)//页面用data接收数据
	public  ResponseEntity<ContentCategory> saveContentCategory(ContentCategory contentCategory){
		try {
			contentCategory.setId(null);
			contentCategory.setIsParent(false);//默认不是父节点
			contentCategory.setStatus(1);
			contentCategory.setSortOrder(1);
			//保存数据
			Boolean boo = this.contentCategoryService.saveContentCategory(contentCategory);
			if (!boo) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			//201
			return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//500
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		
	}
	@RequestMapping(method=RequestMethod.PUT)//页面用data接收数据
	public  ResponseEntity<Void> updateContentCategory(
			@RequestParam("id") Long id,
			@RequestParam("name")String name){
		try {
			ContentCategory record = new ContentCategory();
			record.setId(id);
			record.setName(name);
			this.contentCategoryService.updateByPrimaryKey(record);
			//表示响应执行成功,但没有数据返回204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	@RequestMapping(method=RequestMethod.DELETE)//页面用data接收数据
	public  ResponseEntity<Void> deleteContentCategory(
			@RequestParam("parentId")Long parentId,
			@RequestParam("id") Long id
			){
		try {
			this.contentCategoryService.deleteContentCategoryById(parentId,id);
			//表示响应执行成功,但没有数据返回204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		
	}
}
